package com.bloveoacom.util;

import android.text.TextUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by yjl on 2018/6/14.
 */

public class CalendarUtil {
    /**
     * @param millisUntilFinished second
     * @return
     */
    public static String toTimeBySecond(long millisUntilFinished) {
        long hour = millisUntilFinished / (60 * 60 * 1000);
        long minute = (millisUntilFinished - hour * 60 * 60 * 1000) / (60 * 1000);
        long second = (millisUntilFinished - hour * 60 * 60 * 1000 - minute * 60 * 1000) / 1000;
        if (second >= 60) {
            second = second % 60;
            minute += second / 60;
        }
        if (minute >= 60) {
            minute = minute % 60;
            hour += minute / 60;
        }
        String sh = "";
        String sm = "";
        String ss = "";
        if (hour < 10) {
            sh = "0" + String.valueOf(hour);
        } else {
            sh = String.valueOf(hour);
        }
        if (minute < 10) {
            sm = "0" + String.valueOf(minute);
        } else {
            sm = String.valueOf(minute);
        }
        if (second < 10) {
            ss = "0" + String.valueOf(second);
        } else {
            ss = String.valueOf(second);
        }
        return sh + ":" + sm + ":" + ss;
    }
    static SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
    static SimpleDateFormat format2 = new SimpleDateFormat("MM/dd");
    static SimpleDateFormat format3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static SimpleDateFormat getFormat(int type) {
        SimpleDateFormat sf = null;
        switch (type) {
            case 1:
                sf = format1;
                break;
            case 2:
                sf = format2;
                break;
            case 3:
                sf= format3;
                break;

        }
        return sf;
    }
    public static String getTime(String mills, int type) {
        if (TextUtils.isEmpty(mills)) {
            return "";
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(Long.parseLong(mills));
        String time = getFormat(type).format(calendar.getTime());
        return time;
    }


    public static Calendar getCalendar(String date, int type) {
        if (TextUtils.isEmpty(date)) {
            return Calendar.getInstance();
        }
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(getFormat(type).parse(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return calendar;
    }

    public static String getTime2(String date, int type){

        Calendar calendar = getCalendar(date, type);

        String format = format(calendar.getTime());
        return format;
    }



    private static final String ONE_SECOND_AGO = "秒前";
    private static final String ONE_MINUTE_AGO = "分钟前";
    private static final String ONE_HOUR_AGO = "小时前";
    private static final String ONE_DAY_AGO = "天前";
    private static final String ONE_MONTH_AGO = "月前";
    private static final String ONE_YEAR_AGO = "年前";


    private static final long ONE_MINUTE = 60000L;
    private static final long ONE_HOUR = 3600000L;
    private static final long ONE_DAY = 86400000L;
    private static final long ONE_WEEK = 604800000L;
    public static String format(Date date) {
        long delta = new Date().getTime() - date.getTime();
        if (delta < 1L * ONE_MINUTE) {
            long seconds = toSeconds(delta);
            return (seconds <= 0 ? 1 : seconds) + ONE_SECOND_AGO;
        }
        if (delta < 45L * ONE_MINUTE) {
            long minutes = toMinutes(delta);
            return (minutes <= 0 ? 1 : minutes) + ONE_MINUTE_AGO;
        }
        if (delta < 24L * ONE_HOUR) {
            long hours = toHours(delta);
            return (hours <= 0 ? 1 : hours) + ONE_HOUR_AGO;
        }
        if (delta < 48L * ONE_HOUR) {
            return "昨天";
        }
        if (delta < 30L * ONE_DAY) {
            long days = toDays(delta);
            return (days <= 0 ? 1 : days) + ONE_DAY_AGO;
        }
        if (delta < 12L * 4L * ONE_WEEK) {
            long months = toMonths(delta);
            return (months <= 0 ? 1 : months) + ONE_MONTH_AGO;
        } else {
            long years = toYears(delta);
            return (years <= 0 ? 1 : years) + ONE_YEAR_AGO;
        }
    }

    private static long toSeconds(long date) {
        return date / 1000L;
    }

    private static long toMinutes(long date) {
        return toSeconds(date) / 60L;
    }

    private static long toHours(long date) {
        return toMinutes(date) / 60L;
    }

    private static long toDays(long date) {
        return toHours(date) / 24L;
    }

    private static long toMonths(long date) {
        return toDays(date) / 30L;
    }

    private static long toYears(long date) {
        return toMonths(date) / 365L;
    }

    public static String getMouth(int mouth){
        String mo = "";
        switch (mouth) {
            case 0:
                mo = "一月";
                break;
            case 1:
                mo = "二月";
                break;
            case 2:
                mo = "三月";
                break;
            case 3:
                mo = "四月";
                break;
            case 4:
                mo = "五月";
                break;
            case 5:
                mo = "六月";
                break;
            case 6:
                mo = "七月";
                break;
            case 7:
                mo = "八月";
                break;
            case 8:
                mo = "九月";
                break;
            case 9:
                mo = "十月";
                break;
            case 10:
                mo = "十一月";
                break;
            case 11:
                mo = "十二月";
                break;
        }
        return mo;
    }

    public static String getDay(int day){
        if(day>=10){
            return String.valueOf(day);
        }else{
            return "0"+day;
        }
    }





}
